package com.example.zkweixiu.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.pro.PublicPro;
import com.google.gson.Gson;
import com.yanzhenjie.sofia.Sofia;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class NicknameActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nickname);
        Sofia.with(NicknameActivity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));

        initView();


    }

    private void initView() {
        ImageView iv_fanhui = findViewById(R.id.iv_fanhui);
        View iv_fanhui2 = findViewById(R.id.iv_fanhui2);
        ImageView iv_close = findViewById(R.id.iv_close);
        et_name = findViewById(R.id.et_name);
        Button bt_confirm = findViewById(R.id.bt_confirm);

        iv_fanhui.setOnClickListener(this);
        iv_fanhui2.setOnClickListener(this);
        iv_close.setOnClickListener(this);
        bt_confirm.setOnClickListener(this); //确定按钮


        et_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String name = editable.toString().trim();
                int editEnd = et_name.getSelectionEnd();
                int editStart = et_name.getSelectionStart();
                if (name.length() >= 24) {
                    editable.delete(editStart - 1, editEnd);
                }
            }
        });


        et_name.setText(PublicPro.userBean.getData().getGuaName());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.iv_fanhui2:
                finish();
                break;
            case R.id.iv_close:  //清除输入框内容
                et_name.setText("");
                break;

            case R.id.bt_confirm:
                String trim = et_name.getText().toString().trim();


                if (TextUtils.isEmpty(trim)){
                    Toast.makeText(this, "昵称不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (trim.length() <= 1){
                    Toast.makeText(this, "昵称不能小于两个字", Toast.LENGTH_SHORT).show();
                    return;
                }

                updata(trim);
                break;

        }
    }

    private void updata(String s) {
        OkHttpUtils
                .get()
                .url(PublicPro.UPDATA_NAME)
                .addParams("name", s)
                .addParams("wx_uuid", PublicPro.userBean.getData().getGuaUuid())
                .addParams("token", PublicPro.userBean.getData().getGuaToken())
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);

                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);

                        if ( objBean.getStatus() == 200) {

                            Toast.makeText(NicknameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        } else {

                            Toast.makeText(NicknameActivity.this, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

}
